package com.devgroupvn.dota;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.kin.bitmapcatching.DisplayBitmap.ObserveCatching;
import com.kin.bitmapcatching.ImageDownloader;
import com.kin.bitmapcatching.ImageDownloader.AsyncDownloadDrawable;
import com.kin.bitmapcatching.ImageDownloader.BitmapDownloaderTask;

public class PostAdapter extends BaseAdapter implements ObserveCatching {

	private String[] values;
    private Context context;
    private Activity activity;
	
    private List<Entry> mListData;
	public  PostAdapter( Activity _activity,List<Entry> _List) {
        this.activity =_activity;
        mListData = _List;
	}
	
	private static class  ViewHolder
	{
		TextView textView ;
		TextView tv_des;
		TextView tv_date;
		ImageView ivAvatar ;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mListData.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mListData.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		LayoutInflater inflater = activity.getLayoutInflater();
		Entry entry = mListData.get(position);
		ViewHolder holder = new ViewHolder();
		if(convertView== null)
		{
			convertView = inflater.inflate(R.layout.listpost, null);
			holder.textView = (TextView) convertView.findViewById(R.id.label);
			holder.tv_des = (TextView) convertView.findViewById(R.id.description);
			holder.tv_date= (TextView) convertView.findViewById(R.id.date);
			holder.ivAvatar = (ImageView)convertView.findViewById(R.id.image);
			convertView.setTag(holder);
			
		}
		else
		{
			holder = (ViewHolder)convertView.getTag();
		}
		holder.tv_date .setText(entry.date);
//		holder.tv_des.setText(entry.content);
		holder.textView.setText(entry.title);
		if(entry.mImage!=null && !entry.mImage.trim().equals(""))
		{
			loadBitmapWithMemCache(entry.mImage,holder .ivAvatar );
		}
		return convertView;
	}
	
	@Override
	public void addBitmapToMemCache(String key, Bitmap bitmap) {
		if (getBitmapFromMemCache(key)==null)
    	{
			if(bitmap!=null)
			{
				Application.mMemCache.put(key, bitmap);
			}
    	}
    	
    	if (!Application.mDiskCache.containsKey(key))
    	{
    		if(bitmap!=null)
    		{
    			Application.mDiskCache.put(key, bitmap);	
    		}
    	}
	}

	@Override
	public Bitmap getBitmapFromMemCache(String key) {
		return Application.mMemCache.get(key);
	}

	@Override
	public Bitmap getBitmapFromDiskCache(String key) {
		return Application.mMemCache.get(key);
	}
	
	 private void loadBitmapWithMemCache(String fileName,ImageView imageView)
	    {
	    	if (ImageDownloader.cancelPotentialWork(fileName, imageView))
	    	{
		    	final String imageKey = String.valueOf(fileName);
		    	final Bitmap bitmap = getBitmapFromMemCache(imageKey);
		    	if (bitmap==null)
		    	{
		    		ImageDownloader imageDownloader = new ImageDownloader(activity,this);   		
		    		final BitmapDownloaderTask task = imageDownloader.new BitmapDownloaderTask(imageView,activity);
		    		Bitmap temp = BitmapFactory.decodeResource(activity.getResources(), R.drawable.dotadefault);
		    		final AsyncDownloadDrawable asyncDrawable = new AsyncDownloadDrawable(activity.getResources(),temp, task);
		    		imageView.setImageDrawable(asyncDrawable);
		    		task.execute(fileName);
		    	}
		    	else
		    	{
		    		imageView.setImageBitmap(bitmap);
		    	}
	    	}
	    }
	    
	    
}
